import Vue from 'vue'
import Router from 'vue-router'
import {
  constantRouterMap
} from './router.config.js'

const whiteList = ['/login']
const originalPush = Router.prototype.push
var dingdingStatus = true
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

const createRouter = () =>
  new Router({
    mode: 'history', // 如果你是 history模式 需要配置vue.config.js publicPath
    base: '/mobile',
    // base: process.env.BASE_URL,
    scrollBehavior: () => ({
      y: 0
    }),
    routes: constantRouterMap
  })

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}
// 辅助函数：判断是否需要添加参数
function shouldAddParam(route) {
  // 根据你的逻辑判断是否需要添加参数
  // 例如，检查路由的name或path等
  return !route.params.showmenu
}

// 辅助函数：给路由添加参数
function addParamToRoute(route, paramName, paramValue) {
  return {
    ...route,
    params: {
      ...route.params,
      [paramName]: paramValue
    }
  }
}
router.beforeEach((to, from, next) => {
  let isLogin = Boolean(localStorage.getItem('AuthorizationToken'))

  if (isLogin) {
    // console.log(to.query.showmenu)
    // console.log(to.query)

    next()
    // const newRoute = { ...to, query: { ...to.query, showmenu: false } }
    // next(newRoute)

  } else {

    // 账号密码登录
    if (whiteList.includes(to.path)) {
      next()
    } else {

      next('/login')
    }

  }
})

export default router
